from django.contrib import admin
from django.utils.html import format_html
from .models import Film, ActorToFilm, FilmTag, FilmToPoster, FilmToCompany, ROLE_CHOICES, TAG_TYPE


@admin.register(Film)
class FilmAdmin(admin.ModelAdmin):
    list_display = ('num', 'title', 'get_tags','year', 'rating', 'is_hot', 'mosaic')
    list_filter = ('year', 'countrycode', 'mpaa', 'is_hot', 'mosaic')
    search_fields = ('num', 'title', 'original_title')
    list_editable = ('is_hot', 'mosaic')
    list_per_page = 30
    date_hierarchy = 'premiered'
    autocomplete_fields = ['countrycode', 'tags']

    fieldsets = (
        ('标识信息', {
            'fields': ('num', 'title', 'sort_title', 'original_title')
        }),
        ('剧情描述', {
            'fields': ('plot', 'outline', 'original_plot', 'tagline')
        }),
        ('发行信息', {
            'fields': ('premiered', 'release_date', 'year')
        }),
        ('分级与地区', {
            'fields': ('mpaa', 'countrycode')
        }),
        ('媒体信息', {
            'fields': ('cover_url', 'trailer_url', 'website_url'),
            'classes': ('collapse',)
        }),
        ('统计信息', {
            'fields': ('runtime', 'rating', 'critic_rating', 'votes', 'is_hot', 'mosaic')
        }),
        ('标签信息', {
            'fields': ('tags',),
            'classes': ('collapse',)
        })
    )

    def get_tags(self, obj):
        tags = obj.tags.all()
        return "; ".join(str(tag) for tag in tags)

    get_tags.short_description = '标签'


@admin.register(ActorToFilm)
class ActorToFilmAdmin(admin.ModelAdmin):
    list_display = ('actor', 'film', 'role_display', 'is_representative')
    list_filter = ('role', 'is_representative')
    search_fields = ('actor__name', 'film__title')
    list_editable = ('is_representative',)
    list_per_page = 30
    autocomplete_fields = ['actor', 'film']

    def role_display(self, obj):
        return dict(ROLE_CHOICES).get(obj.role, '')

    role_display.short_description = '角色'


@admin.register(FilmTag)
class FilmTagAdmin(admin.ModelAdmin):
    list_display = ('value', 'tag_type_display')
    list_filter = ('tag_type',)
    search_fields = ('value',)
    list_per_page = 30

    def tag_type_display(self, obj):
        return dict(TAG_TYPE).get(obj.tag_type, '')

    tag_type_display.short_description = '标签类型'


@admin.register(FilmToPoster)
class FilmToPosterAdmin(admin.ModelAdmin):
    list_display = ('film', 'poster_preview', 'type_display')
    list_filter = ('type',)
    search_fields = ('film__title', 'film__num')
    list_per_page = 30

    def poster_preview(self, obj):
        return format_html('', obj.poster.url)

    poster_preview.short_description = '海报预览'

    def type_display(self, obj):
        return dict(FilmToPoster.PHOTO_TYPE).get(obj.type, '')

    type_display.short_description = '类型'


@admin.register(FilmToCompany)
class FilmToCompanyAdmin(admin.ModelAdmin):
    list_display = ('film', 'studio', 'publisher')
    search_fields = ('film__title', 'studio__name', 'publisher__name')
    autocomplete_fields = ['film', 'studio', 'publisher']
    list_per_page = 30
